package com.albert.order.controller;

import com.albert.order.entry.OrderInfo;
import com.albert.order.service.DelayOrderService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 延时订单
 *
 * @author yangjunwei
 * @date 2021/7/3 上午12:39
 */
@Slf4j
@RestController
@RequestMapping("/delayOrder")
public class DelayOrderController {

    @Autowired
    DelayOrderService delayOrderService;

    /**
     * 方案一
     * Java自带的延时队列 - DelayQueue
     *
     * @param orderInfo 订单信息
     */
    @PostMapping("/delayQueue")
    public void createDelayQueue(@RequestBody OrderInfo orderInfo) {
        delayOrderService.createDelayQueue(orderInfo);
    }

    /**
     * 方案二
     * Java自带的延时队列 - DelayQueue
     *
     * @param orderInfo 订单信息
     */
    @PostMapping("/activemq")
    public void snedToActivemq(@RequestBody OrderInfo orderInfo) {
        delayOrderService.sendToActivemq(orderInfo);
    }

    @SneakyThrows
    public static void main(String[] args) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setId(0);
        orderInfo.setOrderStatus(1);
        orderInfo.setOrderName("限时发送的订单");
        orderInfo.setOrderType(1);
        orderInfo.setCreateTime(new Date());
        orderInfo.setAmountMoney(100D);
        orderInfo.setCreateUser("albert");

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(orderInfo);
        System.out.println(s);

    }

}
